<template>
    <el-table class="mt20" :data="tableData" border v-loading="form.loading" style="width: 100%;font-size: 16px;">
        <el-table-column type="index" :index="indexMethod" label="序号" width="60" align="center"  />
        <el-table-column label="所属项目" show-overflow-tooltip align="center" >
            <template #default="scope">
                <el-text>{{scope.row.project.title}}</el-text>
            </template>
        </el-table-column>
        <el-table-column label="申请人" show-overflow-tooltip align="center" >
            <template #default="scope">
                <el-text>{{scope.row.user.name}}</el-text>
            </template>
        </el-table-column>
        <el-table-column prop="created_at" label="申请时间" width="180" align="center"  />
        <el-table-column fixed="right" label="操作" width="80" align="center" >
            <template #default="scope">
                <el-link type="primary" v-if="scope.row.status == 'pending'" @click="onEdit(scope.row)">审批</el-link>
                <el-link v-else link type="primary" @click="onEdit(scope.row)">查看</el-link>
            </template>
        </el-table-column>
    </el-table>
    <div class="mt20">
        <el-pagination v-model:current-page="form.current" v-model:page-size="form.limit" :page-sizes="[10, 20, 30, 40, 50, 100]" layout="total, sizes, prev, pager, next, jumper" :total="form.total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>

    <el-dialog v-model="isEdit" title="采购合同申请" width="900px">
        <component :is="contractForm" :data = sourceId :type="true"/>
        <component :is="approveForm" :data = editId @onSave="onSave" />
    </el-dialog>

</template>

<script lang="ts" setup>
import { ref, watch } from 'vue'
import contractForm from '@/components/contractForm.vue';
import approveForm from '../components/approveForm.vue';
import http from '../../../../util/request';
const indexMethod = (index: number) => {
  return index+= 1
}
const form = ref<any>({search:'',source:'',model:'Contract',page:0,current:0,total:10,limit:20,loading:false,parentBorder:false})
const isEdit = ref<boolean>(false)
const editId = ref<number>(0)
const sourceId = ref<number>(0)
const tableData = ref<any[]>([])


const onGetData = () => {
    
    form.value.loading = true
    tableData.value = []
    http.post('/approve',form.value).then((response:any)=>{
        form.value.loading = false
        tableData.value = response.data.data
        form.value.total = response.data.total
    })
}

const onEdit = (data: any) => {
    editId.value = data.id
    sourceId.value = data.source_id
    isEdit.value = true
}

const handleSizeChange = (val: number) => {
    form.value.limit = val
    onGetData()
}
const handleCurrentChange = (val: number) => {
    form.value.page = val
    onGetData()
}


const props = defineProps<{
    source: string
}>()

watch(props,()=>{
    form.value.source = props.source
    onGetData()
},{ immediate: true })

const onSave = () => {
    editId.value = 0
    isEdit.value = false
    onGetData()
}

</script>

<style scoped>

</style>